const communityDao = require('../dao/communityDao');

const Response = require('../entity/http/Response');

const communityService = {
    /**
     * 获取动态类型接口
     */
    async getPostTypes() {
        try {
            result = await communityDao.getPostTypes();
            result = (new Response()).ok("操作成功", result);
        } catch (err) {
            result = (new Response()).fail(500, "资源不可访问");
        }
        return result;
    },
    /**
     * 预发布动态
     */
    async preparePost(uid, preparePost) {
        let { title, content, imgCount, type } = preparePost;
        let result;
        try {
            result = await communityDao.preparePost(uid, title, content, imgCount, type);
            result = (new Response()).ok("操作成功", result);
        } catch (err) {
            result = (new Response()).fail(500, "资源不可访问");
        }
        return result;
    },
    /**
     * 标记发布动态
     */
    async finalizePost(uid, postId) {
        let result;
        try {
            result = await communityDao.finalizePost(uid, postId);
            if (result.changedRows > 0) {
                result = (new Response()).ok("操作成功", {});
            } else {
                result = (new Response()).fail(500, "本次操作无效");
            }
        } catch (err) {
            result = (new Response()).fail(500, "资源不可访问");
        }
        return result;
    },
    /**
     * 标记图片上传完毕
     */
    async finalizeImg(uid, imgId) {
        let result;
        try {
            result = await communityDao.finalizeImg(uid, imgId);
            if (result.changedRows > 0) {
                result = (new Response()).ok("操作成功", {});
            } else {
                result = (new Response()).fail(500, "本次操作无效");
            }
        } catch (err) {
            result = (new Response()).fail(500, "资源不可访问");
        }
        return result;
    },
    /**
     * 删除动态
     */
    async delPost(uid, postId) {
        let result;
        try {
            result = await communityDao.delPost(uid, postId);
            if (result.changedRows > 0) {
                result = (new Response()).ok("操作成功", {});
            } else {
                result = (new Response()).fail(500, "该动态不存在");
            }
        } catch (err) {
            result = (new Response()).fail(500, "资源不可访问");
        }
        return result;
    },
    /**
     * 获取推荐动态
     */
    async getRecommendPosts(uid, postId) {
        let result;
        try {
            result = await communityDao.getRecommendPosts(uid, postId);
            for (let i = 0; i < result.length; i++) {
                result[i].imgIdList = JSON.parse(result[i].imgIdList);
                result[i].imgList = [];
                for (let j = 0; j < result[i].imgIdList.length; j++) {
                    let imgId = result[i].imgIdList[j];
                    let url = await communityDao.getCommentImgUrl(imgId);
                    result[i].imgList.push(url);
                }
            }
            result = (new Response()).ok("操作成功", result);
        } catch (err) {
            result = (new Response()).fail(500, "资源不可访问");
        }
        return result;
    },
    // 按照类型懒加载获取动态
    async getRecommendPostsByType(uid, postId, typeId) {
        let result;
        try {
            result = await communityDao.getRecommendPostsByType(uid, postId, typeId);
            for (let i = 0; i < result.length; i++) {
                result[i].imgIdList = JSON.parse(result[i].imgIdList);
                result[i].imgList = [];
                for (let j = 0; j < result[i].imgIdList.length; j++) {
                    let imgId = result[i].imgIdList[j];
                    let url = await communityDao.getCommentImgUrl(imgId);
                    result[i].imgList.push(url);
                }
            }
            result = (new Response()).ok("操作成功", result);
        } catch (err) {
            result = (new Response()).fail(500, "资源不可访问");
        }
        return result;
    },
    // 获取自己发布的动态
    async getSelfPosts(uid, lastPostId) {
        let result;
        try {
            result = await communityDao.getSelfPosts(uid, lastPostId);
            for (let i = 0; i < result.length; i++) {
                result[i].imgIdList = JSON.parse(result[i].imgIdList);
                result[i].imgList = [];
                for (let j = 0; j < result[i].imgIdList.length; j++) {
                    let imgId = result[i].imgIdList[j];
                    let url = await communityDao.getCommentImgUrl(imgId);
                    result[i].imgList.push(url);
                }
            }
            result = (new Response()).ok("操作成功", result);
        } catch (err) {
            result = (new Response()).fail(500, "资源不可访问");
        }
        return result;
    },
    /**
     * 获取单个动态
     */
    async getPost(uid, postId) {
        let result;
        try {
            result = await communityDao.getPost(uid, postId);
            if (result.length) {
                result = result[0];
                result.imgIdList = JSON.parse(result.imgIdList);
                result.imgList = [];
                for (let j = 0; j < result.imgIdList.length; j++) {
                    let imgId = result.imgIdList[j];
                    let url = await communityDao.getCommentImgUrl(imgId);
                    result.imgList.push(url);
                }
                result = (new Response()).ok("操作成功", result);
            } else {
                result = (new Response()).fail(500, "该动态不存在");
            }
        } catch (err) {
            result = (new Response()).fail(500, "资源不可访问");
        }
        return result;
    },
    // 点赞或取消点赞动态
    async likePost(post, uid) {
        const { id, like } = post;
        let result;
        try {
            result = await communityDao.likePost(id, like, uid);
            if (result.affectedRows) {
                result = (new Response()).ok("操作成功");
            } else {
                result = (new Response()).fail(500, "操作失败");
            }
        } catch (err) {
            result = (new Response()).fail(500, "资源不可访问");
        }
        return result;
    },
    async postComment(comment, uid) {
        const { id, content } = comment;
        let result;
        try {
            result = await communityDao.postComment(id, content, uid);
            if (result.insertId) {
                result = (new Response()).ok("评论成功", result.insertId);
            } else {
                result = (new Response()).fail(500, "评论失败");
            }
        } catch (err) {
            result = (new Response()).fail(500, "资源不可访问");
        }
        return result;
    },
    async postCommentReply(comment, uid) {
        const { id, content } = comment;
        let result;
        try {
            result = await communityDao.postCommentReply(id, content, uid);
            if (result.insertId) {
                result = (new Response()).ok("评论成功", result.insertId);
            } else {
                result = (new Response()).fail(500, "评论失败");
            }
        } catch (err) {
            result = (new Response()).fail(500, "资源不可访问");
        }
        return result;
    },
    async getPostComment(postId, lastCommentId) {
        let result;
        try {
            result = await communityDao.getPostComment(postId, lastCommentId);
            result = (new Response()).ok("获取成功", result);
        } catch (err) {
            result = (new Response()).fail(500, "资源不可访问");
        }
        return result;
    },
    async getPostCommentReply(postCommentId, lastCommentId) {
        let result;
        try {
            result = await communityDao.getPostCommentReply(postCommentId, lastCommentId);
            result = (new Response()).ok("获取成功", result);
        } catch (err) {
            result = (new Response()).fail(500, "资源不可访问");
        }
        return result;
    },
    async deleteComment(uid, id) {
        let result;
        try {
            result = await communityDao.deleteComment(uid, id);
            if (result.affectedRows) {
                result = (new Response()).ok("删除成功");
            } else {
                result = (new Response()).fail(500, "评论是你的吗你就删");
            }
        } catch (err) {
            result = (new Response()).fail(500, "资源不可访问");
        }
        return result;
    },
    async deleteCommentReply(uid, id) {
        let result;
        try {
            result = await communityDao.deleteCommentReply(uid, id);
            if (result.affectedRows) {
                result = (new Response()).ok("删除成功");
            } else {
                result = (new Response()).fail(500, "评论是你的吗你就删");
            }
        } catch (err) {
            result = (new Response()).fail(500, "资源不可访问");
        }
        return result;
    }
};

module.exports = communityService;